package org.ms.mtx.example.service;

import org.apache.commons.lang3.StringUtils;
import org.ms.mtx.core.annotation.Transactional;
import org.ms.mtx.example.service.cannel.ScoreCancelService;
import org.springframework.stereotype.Service;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * 积分服务
 *
 * @author lry
 */
@Service
public class ScoreService {

    /**
     * 模拟积分
     */
    public static AtomicInteger score = new AtomicInteger(0);

    @Transactional(cancelClass = ScoreCancelService.class)
    public Boolean addScore(Integer tempScore, String exceptionMock) {
        if (StringUtils.isNotBlank(exceptionMock) && exceptionMock.equals(Thread.currentThread().getStackTrace()[1].getMethodName())) {
            throw new RuntimeException("模拟异常:" + Thread.currentThread().getStackTrace()[1].getMethodName());
        }

        if (tempScore <= 0) {
            throw new RuntimeException("增加的积分不能小于等于0");
        } else {
            System.out.println("增加前积分:" + score.get());
        }

        return true;
    }

    public Boolean addScoreConfirm(Integer tempScore, String exceptionMock) {
        if (StringUtils.isNotBlank(exceptionMock) && exceptionMock.equals(Thread.currentThread().getStackTrace()[1].getMethodName())) {
            throw new RuntimeException("模拟异常:" + Thread.currentThread().getStackTrace()[1].getMethodName());
        }

        System.out.println("增加积分:积分从[" + score.get() + "]增加为[" + score.addAndGet(tempScore) + "]");
        return true;
    }

}
